import 'dart:convert';

import 'package:dynamic_stock/const/const.dart' show USER_CACHE_KEY;
import 'package:shared_preferences/shared_preferences.dart';

class LoginModel {
  int errcode;
  UserData data;
  String message;
  int retcode;

  LoginModel({this.errcode, this.data, this.message, this.retcode});

  LoginModel.fromJson(Map<String, dynamic> json) {
    errcode = json['errcode'];
    data = json['data'] != null ? new UserData.fromJson(json['data']) : null;
    message = json['message'];
    retcode = json['retcode'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['errcode'] = this.errcode;
    if (this.data != null) {
      data['data'] = this.data.toJson();
    }
    data['message'] = this.message;
    data['retcode'] = this.retcode;
    return data;
  }
}
class UserData {

  String platUserId;
  String platUserAccount;
  String platUserAvatar;
  int platUserSource;
  String updateUser;
  int platTradeAccountType;
  String updateTime;
  String platMobilePhone;
  String platUserCreateUser;
  String platUserPassword;
  String platComName;
  String platIdentityNo;
  int platUserState;
  String platComId;
  String platUserNickName;
  String platRegisterTime;
  String createTime;
  String platUserName;
  int platUserLevel;
  String createUser;
  String platUserToken;
  int id;
  String platAlipayAccount;
  String platAlipayUserName;

  UserData(
      {this.platUserId,
      this.platUserAccount,
      this.platUserAvatar,
      this.platUserSource,
      this.updateUser,
      this.platTradeAccountType,
      this.updateTime,
      this.platMobilePhone,
      this.platUserCreateUser,
      this.platUserPassword,
      this.platComName,
      this.platIdentityNo,
      this.platUserState,
      this.platComId,
      this.platUserNickName,
      this.platRegisterTime,
      this.createTime,
      this.platUserName,
      this.platUserLevel,
      this.createUser,
      this.platUserToken,
      this.id,
      this.platAlipayAccount,
      this.platAlipayUserName});

  UserData.fromJson(Map<String, dynamic> json) {
    platUserId = json['platUserId'];
    platUserAccount = json['platUserAccount'];
    platUserAvatar = json['platUserAvatar'];
    platUserSource = json['platUserSource'];
    updateUser = json['updateUser'];
    platTradeAccountType = json['platTradeAccountType'];
    updateTime = json['updateTime'];
    platMobilePhone = json['platMobilePhone'];
    platUserCreateUser = json['platUserCreateUser'];
    platUserPassword = json['platUserPassword'];
    platComName = json['platComName'];
    platIdentityNo = json['platIdentityNo'];
    platUserState = json['platUserState'];
    platComId = json['platComId'];
    platUserNickName = json['platUserNickName'];
    platRegisterTime = json['platRegisterTime'];
    createTime = json['createTime'];
    platUserName = json['platUserName'];
    platUserLevel = json['platUserLevel'];
    createUser = json['createUser'];
    platUserToken = json['platUserToken'];
    id = json['id'];
    platAlipayAccount = json['platAlipayAccount'];
    platAlipayUserName = json['platAlipayUserName'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['platUserId'] = this.platUserId;
    data['platUserAccount'] = this.platUserAccount;
    data['platUserAvatar'] = this.platUserAvatar;
    data['platUserSource'] = this.platUserSource;
    data['updateUser'] = this.updateUser;
    data['platTradeAccountType'] = this.platTradeAccountType;
    data['updateTime'] = this.updateTime;
    data['platMobilePhone'] = this.platMobilePhone;
    data['platUserCreateUser'] = this.platUserCreateUser;
    data['platUserPassword'] = this.platUserPassword;
    data['platComName'] = this.platComName;
    data['platIdentityNo'] = this.platIdentityNo;
    data['platUserState'] = this.platUserState;
    data['platComId'] = this.platComId;
    data['platUserNickName'] = this.platUserNickName;
    data['platRegisterTime'] = this.platRegisterTime;
    data['createTime'] = this.createTime;
    data['platUserName'] = this.platUserName;
    data['platUserLevel'] = this.platUserLevel;
    data['createUser'] = this.createUser;
    data['platUserToken'] = this.platUserToken;
    data['id'] = this.id;
    data['platAlipayAccount'] = this.platAlipayAccount;
    data['platAlipayUserName'] = this.platAlipayUserName;
    return data;
  }

  static Future<UserData> instance() async {
    SharedPreferences share = await SharedPreferences.getInstance();
    String json = share.getString(USER_CACHE_KEY);
    if(json != null){
      UserData userData = UserData.fromJson(jsonDecode(json));
      return userData;
    }

    return null;
  }
}